<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author XPF
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    :okAuth="'sports:sportsScoreGrade:edit'"
    title="已选评分规则"
    width="90%"
  >
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="handleForm()" v-auth="'sports:sportsScoreGrade:edit'">
          <Icon icon="fluent:add-12-filled" /> {{ t('选择规则') }}
        </a-button>
      </template>
      <template #scoreGrade="{ column, record }">
        <DictLabel
          v-if="record.sportsScoreGrade"
          :dictType="column.dictType"
          :dictValue="record.sportsScoreGrade.scoreGrade"
        />
      </template>
    </BasicTable>
  </BasicDrawer>
  <ScoreGradeModal @register="registerScoreGradeModal" @success="handleSuccess" />
</template>

<script lang="ts">
  export default defineComponent({
    name: 'ViewSportsSportsWeightTable',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useModal } from '/@/components/Modal';
  import {
    SportsScoreGrade,
    sportsScoreGradeSelectSingleIndex,
    cancelSingleIndex,
  } from '/@/api/sports/sportsScoreGrade';
  import ScoreGradeModal from './ScoreGradeModal.vue';
  import { DictLabel } from '/@/components/Dict';
  const { showMessage } = useMessage();
  const { t } = useI18n('sports.sportsWeight');
  const record = ref<SportsScoreGrade>({} as SportsScoreGrade);
  const tableColumns: BasicColumn[] = [
    {
      title: t('指标名称'),
      dataIndex: 'sportsWeight.indexName',
      key: 's_index_name',
      //sorter: true,
      align: 'left',
      slots: { customRender: 'firstColumn' },
    },
    {
      title: t('适配年级'),
      dataIndex: 'scoreAdaptGrade',
      key: 's_score_adapt_grade',
      //sorter: true,
      align: 'center',
      dictType: 'vke_grade',
    },
    {
      title: t('适配性别'),
      dataIndex: 'scoreAdaptSex',
      key: 'a.score_adapt_ex',
      //sorter: true,
      align: 'left',
      dictType: 'sys_user_sex',
    },
    // {
    //   title: t('符号'),
    //   dataIndex: 'scoreSymbol',
    //   key: 's.score_sumbol',
    //   //sorter: true,
    //   align: 'left',
    //   dictType: 's_scoring_rules',
    // },
    {
      title: t('分值'),
      dataIndex: 'score1',
      key: 'a.adapt_sex',
      //sorter: true,
      align: 'left',
    },
    {
      title: t('等级'),
      dataIndex: 'sportsScoreGrade.scoreGrade',
      key: 'a.adapt_Grade',
      //sorter: true,
      dictType: 's_score_grade',
      slots: { customRender: 'scoreGrade' },
    },
  ];
  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除单项指标模块'),
        //label: t('取消'),
        popConfirm: {
          title: t('是否确认取消已选评分规则'),
          confirm: onCancel.bind(this, { scoreId: record.scoreId }),
        },
        auth: 'sports:sportsScoreGrade:edit',
      },
    ],
  };
  let dataTable;
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner((data) => {
    dataTable = data;
    reload();
  });
  let keyArr=[]
  const [registerTable, { reload }] = useTable({
    api: async (params) => {
      params.scoreGradeId = dataTable.scoreGradeId;
      const res = await sportsScoreGradeSelectSingleIndex(params);
      // if (res.data[0].score2 != '') {
      //   res.data[0].score1 = res.data[0].score1 + '-' + res.data[0].score2;
      // }
      res.list.forEach((item)=>{
        keyArr.push(item.scoreId)
      })
      return res;
    },
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    // showTableSetting: true,
    canResize: true,
  });
  const [registerScoreGradeModal, { openModal }] = useModal();

  function handleForm(record: Recordable = dataTable) {
    console.log(record);
    openModal(true, { record, keyArr });
  }
  async function handleDelete(record: Recordable) {
    const res = await sportsAverageScoreDelete(record);
    showMessage(res.message);
    handleSuccess();
  }
  async function onCancel(record: Recordable) {
    const res = await cancelSingleIndex(record);
    handleSuccess();
  }
  function handleSuccess() {
    reload();
  }
</script>
